package com.controller;

import com.bean.Paper;
import com.bean.PaperChoice;
import com.bean.PaperCustom;
import com.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;
import java.util.zip.InflaterInputStream;

/**
 * Created by Administrator on 2016/12/23.
 */
@Controller
@RequestMapping("paper")
public class PaperController {
    @Autowired
    PaperService paperService;

    @RequestMapping("/insertPaperTitle")
    public String insertPaperTitle(HttpServletRequest request){
        Long teacherId = Long.parseLong(request.getParameter("userid"));
        String paperTitle = request.getParameter("paperTitle");
        Paper paper = new Paper();
        paper.setTeacherId(teacherId);
        paper.setPaperTitle(paperTitle);
        paperService.insertPaperTitle(paper);
        return "redirect:/submenu/paper-teacher.do";
    }

    @RequestMapping("/updatePaperMsgByStudent")
    public String updatePaperMsgByStudent(HttpServletRequest request){
        Long id = Long.parseLong(request.getParameter("id"));
        Long teacherId = Long.parseLong(request.getParameter("teacherid"));
        PaperChoice paperChoice = new PaperChoice();
        paperChoice.setId(id);
        paperChoice.setTeacherId(teacherId);
        paperService.updatePaperTeacherId(paperChoice);
        return "redirect:/submenu/paper-list.do";
    }
    @RequestMapping("/insertPaperTeacherByStudent")
    public String insertPaperTeacherByStudent(HttpServletRequest request){
        Long teacherid = Long.parseLong(request.getParameter("teacherid"));
        Long studentid = Long.parseLong(request.getParameter("studentid"));
        PaperChoice paperChoice = new PaperChoice();
        paperChoice.setStudentId(studentid);
        paperChoice.setTeacherId(teacherid);
        if (teacherid!=0){
            paperService.insertStudentIdAndTeacherId(paperChoice);
        }
        return "redirect:/submenu/paper-list.do";
    }

    @RequestMapping("/updatePaperTitleById")
    public String updatePaperTitleById(HttpServletRequest request){
        Long paperid = Long.parseLong(request.getParameter("paperid"));
        String paperTitle = request.getParameter("paperTitle");
        Paper paper = new Paper();
        paper.setId(paperid);
        paper.setPaperTitle(paperTitle);
        paperService.updatePaperTitleById(paper);
        return "redirect:/submenu/paper-teacher.do";
    }
    @RequestMapping("/delPaperById")
    public String delPaperById(HttpServletRequest request){
        Long id = Long.parseLong(request.getParameter("paperid"));
        paperService.delPaperById(id);
        return "redirect:/submenu/paper-teacher.do";
    }
    @RequestMapping("/studentAddTitle")
    public String studentAddTitle(HttpServletRequest request){
        Long userid = Long.parseLong(request.getParameter("userid"));
        List<PaperChoice> teacherid = paperService.getTeacherIdByStudent(userid);
        String paperTitle = request.getParameter("paperTitle");
        Paper paper = new Paper();
        paper.setTeacherId(teacherid.get(0).getTeacherId());
        paper.setPaperTitle(paperTitle);
        paperService.insertPaperTitleByStudent(paper);

        PaperCustom paperCustom = new PaperCustom();
        paperCustom.setStudentId(userid);
        paperCustom.setPaperTitle(paperTitle);
        paperService.updatePaperTitleByStudent(paperCustom);
        return "redirect:/submenu/paper-student-list.do";
    }

    @RequestMapping("/paper-student-del")
    public String paperStudentDel(HttpServletRequest request){
        Long userid = Long.parseLong(request.getParameter("userid"));
        paperService.delPaperTitleByStudent(userid);
        return "redirect:/submenu/paper-student-list.do";
    }
    @RequestMapping("/paper-student-choice")
    public String paperStudentChoice(HttpServletRequest request){
        Long userid = Long.parseLong(request.getParameter("userid"));
        Long paperTitleId = Long.parseLong(request.getParameter("paperTitleId"));
        PaperChoice paperChoice = new PaperChoice();
        paperChoice.setStudentId(userid);
        paperChoice.setTitleId(paperTitleId);
        paperService.updatePaperTitle(paperChoice);
        return "redirect:/submenu/paper-student-list.do";
    }
    @RequestMapping("/updateReviewAndGrade")
    public String updateReviewAndGrade(HttpServletRequest request){
        Long paperchoiceid = Long.parseLong(request.getParameter("paperchoiceid"));
        String paperReview = request.getParameter("paperReview");
        int grade = Integer.parseInt(request.getParameter("grade"));
        List<PaperChoice> list = paperService.getTitleId(paperchoiceid);
        Long titleId = list.get(0).getTitleId();
        Paper paper = new Paper();
        paper.setId(titleId);
        paper.setPaperReview(paperReview);
        paperService.updatePaperReview(paper);

        PaperChoice paperChoice = new PaperChoice();
        paperChoice.setId(paperchoiceid);
        paperChoice.setGrade(grade);
        paperService.updateReviewAndGrade(paperChoice);

        return "redirect:/submenu/paper-teacher.do";
    }
    @RequestMapping("/updatePaperTeacherByStudent")
    public String updatePaperTeacherByStudent(HttpServletRequest request){
        Long choiceid = Long.parseLong(request.getParameter("choiceid"));
        Long teacherid = Long.parseLong(request.getParameter("teacherid"));
        PaperCustom paperCustom = new PaperCustom();
        paperCustom.setId(choiceid);
        paperCustom.setTeacherId(teacherid);
        paperService.updatePaperTeacherByStudent(paperCustom);
        return "redirect:/submenu/paper-list.do";
    }
    //学生提交论文
    @RequestMapping("/addPaperByStudent")
    public String addPaperByStudent(@RequestParam(value = "file_doc", required = false)MultipartFile file, HttpServletRequest request){
        Long studentId = Long.parseLong(request.getParameter("userid"));
        Long titleId = paperService.getTitleIdByStudentId(studentId).get(0).getTitleId();

        //上传论文
        System.out.println("开始");
        String path = request.getSession().getServletContext().getRealPath("upload");
        String fileName=file.getOriginalFilename();
        System.out.println(path);
        File targeFile = new File(path,fileName);
        if(!targeFile.exists()){
            targeFile.mkdirs();
        }
        try {
            file.transferTo(targeFile);
        }catch (Exception e){
            e.printStackTrace();
        }
        //将上传的论文的保存路径存入到数据库中
        Paper paper = new Paper();
        paper.setId(titleId);
        paper.setPaper(path+"\\"+fileName);
        paperService.updatePaper(paper);

        return "redirect:/submenu/paper-student-list.do";

    }
}
